package com.itheima.myset;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
/*
TreeSet实现类三大特点
1.没有索引  2.元素不能重复   C

 */
public class Demo1 {
    public static void main(String[] args) {
        Set<String> set = new TreeSet<>();
        set.add("aaa");
        set.add("bbb");
        set.add("aaa");
        set.add("ccc");
        set.add("ddd");
        System.out.println(set);
        System.out.println("----------------");
        Set<Integer> set1 = new TreeSet<>();
        set1.add(1);
        set1.add(2);
        set1.add(5);
        set1.add(4);
        System.out.println(set1);
        System.out.println("----------------");
        //Student cannot be cast to java.base/java.lang.Comparable
        Student stu1 = new Student("张三",45);
        Student stu2 = new Student("李四",10);
        Student stu3 = new Student("王五",21);
        Student stu4 = new Student("赵六",10);
        Student stu5 = new Student("孙七",13);
        Set<Student> set2 = new TreeSet<>();
        set2.add(stu1); //.Student cannot be cast to java.base/java.lang.Comparable学生不能进行比较
        set2.add(stu2);
        set2.add(stu3);
        set2.add(stu4);
        set2.add(stu5);
        System.out.println(set2);

    }
}
class Student implements Comparable<Student>{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        int res = this.age - o.age;
        int res1 = res == 0 ? this.name.compareTo(o.name) : res;
        return res1;
    }
}